<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- default-expand-all 组件带的属性 控制页面默认状态是否展开 -->
        <!-- <el-tree
          default-expand-all
          :data="list"
          :props="{
            children: 'children',
            label: 'name'
          }"
        /> -->
        <el-tree
          default-expand-all
          :data="list"
        >
          <!--通过默认作用域插槽获取组件内的内容 -->
          <template #default="{data}">
            <el-row type="flex" justify="space-between" align="middle" style="height:40px; width: 100%;">
              <el-col :span="20">
                <svg-icon icon-class="home" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children.length===0" @click.native="delEdit(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>

      <el-dialog
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :title="isEdit? '编辑部门':'新增部门'"
        :visible.sync="dialogVisible"
        width="50%"
        @closed="hClosed"
      >
        <addOrEdit
          :id="culId"
          ref="deptRef"
          :orgin-list="orginList"
          :is-edit="isEdit"
          @success="close"
          @hSuccess="dialogVisible=false"
        />
        <span slot="footer" class="dialog-footer" />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getDepartments, delDepartments } from '@/api/departments'
import { transDataToTreeData } from '@/utils/index'
import addOrEdit from './dialog.vue'
export default {
  components: {
    addOrEdit
  },
  data() {
    return {
      list: [],
      dialogVisible: false,
      culId: '', // 存放传递到子组件中的id
      isEdit: true, // 设置一个变量 false表示新增部门，true表示编辑部门
      orginList: [] // 保存传递给子组件的部门列表
    }
  },
  created() {
    // 调用函数
    this.logoDepartments()
  },
  methods: {
    hClosed() {
      this.$refs.deptRef.resetForm()
    },
    // 删除部门
    delEdit(id) {
      this.$confirm('确定删除部门吗, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await delDepartments(id)
        if (res.code === 10000) {
          this.$message.success(res.message)
          // 重新渲染页面
          this.logoDepartments()
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    hEdit(id) {
      this.culId = id
      this.isEdit = true
      // 解决页面不跟新
      this.$nextTick(() => {
        this.$refs.deptRef.loadDepartments()
      })
      this.dialogVisible = true
    },
    close() {
      // 关闭弹框
      this.dialogVisible = false
      // 重新加载页面
      this.logoDepartments()
    },
    hAdd(id) {
      this.isEdit = false
      this.culId = id
      this.dialogVisible = true
    },
    // 发送axios 得到数据
    async logoDepartments() {
      const res = await getDepartments()
      // console.log(res)
      if (res.code !== 10000) return this.$message.error(res.message)
      this.orginList = res.data.depts.map(item => {
        const { id, name, code, pid } = item
        return { id, name, code, pid }
      })
      // 删除数据的第一条
      res.data.depts.shift()
      this.list = transDataToTreeData(res.data.depts)
    }
  }
}
</script>
